##Replication code for "drivers of resemblance: explaining the conversion of pre-electoral coalitions
##into coalition cabinets"
library(QCA)
library(tidyverse)
library(ragg)
library(SetMethods)
library(showtext)

sec <- read.csv("dpr.csv", sep=";")

##Calibration Process----
sec1 <- sec %>%
        filter(type == "pre") %>%
        select(-type, -legislative_type, -inter_party_primaries,
               -low_chamber, -upp_chamber, -status, -country, -ce) %>%
        column_to_rownames(var = "cases")

sec2 <- sec1 %>%
        mutate(lwpol = calibrate(lwpol, type = "fuzzy", method = "direct", threshold = "e=3, c=2, i=1")) %>%
        mutate(hlpol = calibrate(hlpol, type = "fuzzy", method = "direct", threshold = "e=1.5, c=2.7, i=4")) %>%
        mutate(htemp = calibrate(htemp, type = "fuzzy", method = "direct", threshold = "e=85, c=70, i=55")) %>%
        mutate(lplp = calibrate(lplp, type = "fuzzy", method = "direct", threshold = "e=0.5, c=0.405, i=0.3")) %>%
        rename(LWPOL = lwpol,
               MAJ = maj, 
               HLPOL = hlpol,
               HTEMP = htemp,
               LPP = lplp,
               CR = cr)

##Analysis of Necessity----
superSubset(sec2, 
            outcome = "CR",
            incl.cut = 0.9,
            ron.cut = 0.6)

##XY Plot for Necessity----
#standard version
sol <- "MAJ + HTEMP"

##This produces a raw plot from the QCA package
XYplot(sol, "CR" , data = sec2, enhance = T, relation = "necessity", jitter = T, 
       xlab = "MAJ + HTEMP", ylab = "CR", clabels = rownames(sec2),
       family = "regular",
       cex = 1)

#Fonts
##Please download the fonts below on https://fonts.google.com/specimen/Barlow+Semi+Condensed to reproduce the figure in its entirety.
font_add(family = "regular", "Barlow Semi Condensed-Regular.ttf")
font_add(family = "bold", "BarlowSemiCondensed-SemiBold.ttf")
showtext_auto()   

#Plot
##This produces Figure A.1 in the Supplementary Material
xyplot <- sec2 %>% mutate(sol = pmax(MAJ, HTEMP),
                          tosee = CR > sol,
                          rownames = c("De La Rua", "", "", "Paz Zamora", "Banzer", "Cardoso I", "",
                                       "Lula I", "Lula II", "", "", "Aylwin", "Frei", "Lagos", "", "Pinera I",
                                       "", "Pinera II", "Uribe II", "", "", "", "Balladares", "Moscoso", "",
                                       "", "Varela", "", "", "Caldera", "Chavez"),
                          interest = case_when(rownames == "Cardoso I" ~ "Deviant Consistency in Kind" ,
                                               rownames == "Moscoso" ~ "Deviant Consistency in Kind",
                                               rownames == "De La Rua" ~ "Deviant Consistency in Degree",
                                               rownames == "Aylwin" ~ "Deviant Consistency in Degree",
                                               rownames == "Frei" ~ "Deviant Consistency in Degree",
                                               rownames == "Lagos" ~ "Deviant Consistency in Degree",
                                               rownames == "Pinera I" ~ "Deviant Consistency in Degree",
                                               rownames == "Pinera II" ~ "Deviant Consistency in Degree",
                                               rownames == "Uribe II" ~ "Deviant Consistency in Degree",
                                               rownames == "Caldera" ~ "Deviant Consistency in Degree",
                                               rownames == "Chavez" ~ "Deviant Consistency in Degree",
                                               rownames == "Balladares" ~ "Individually Irrelevant",
                                               rownames == "Paz Zamora" ~ "Individually Irrelevant",
                                               rownames == "Lula I" ~ "Deviant Coverage",
                                               rownames == "Lula II" ~ "Deviant Coverage",
                                               rownames == "Banzer" ~ "Deviant Coverage",
                                               rownames == "Varela" ~ "Deviant Coverage",
                          ),
                          interest = replace_na(interest, "Typical"),
                          interest = factor(interest, levels = c("Deviant Consistency in Kind", "Deviant Consistency in Degree", "Deviant Coverage",  "Individually Irrelevant", "Typical")),
                          rownames = c("", "", "", "", "", "Cardoso I", "",
                                       "", "", "", "", "", "", "", "", "",
                                       "", "", "", "", "", "", "", "Moscoso", "",
                                       "", "", "", "", "", "")) %>%
  ggplot(aes(sol, CR)) +
  geom_point(aes(colour = interest, fill = interest, shape = interest), size = 2, position = "jitter") +
  geom_text_repel(aes(label = rownames), size = 12, family = "regular") +
  geom_abline(intercept = 0, slope = 1) +
  geom_hline(yintercept = 0.5, linetype = 2) +
  geom_vline(xintercept = 0.5, linetype = 2) +
  coord_cartesian(expand=FALSE) +
  scale_color_manual(values = c("#E8080c","#808080", "#bb517d","#c0c0c0", "#90d5e8")) +
  scale_fill_manual(values = c("#E8080c","#808080", "#bb517d","#c0c0c0", "#90d5e8")) +
  scale_shape_manual(values = c(23, 19, 19, 19, 19))+
  scale_x_continuous(limits = c(-0.02, 1.02),
                     breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) +
  scale_y_continuous(limits = c(-0.02, 1.02),
                     breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) +
  xlab("MAJ + HTEMP") + ylab("CR") +
  labs(title = "Necessity Relation",
       subtitle = "Inclusion: 0.916 - Coverage: 0.859 - Relevance: 0.601",
       shape = "Types of Cases",
       colour = "Types of Cases",
       fill = "Types of Cases") +
  guides(fill=guide_legend(ncol = 3) ,
         colour=guide_legend(ncol = 3),
         shape=guide_legend(ncol = 3)) +
  theme(
    #Title, Subtitle, Caption
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 45, color = "black"),
    plot.title.position = "plot",
    plot.subtitle = element_text(family="regular", size = 40, hjust = 0.5, color = "black"),
    #Panel and Background
    panel.background = element_rect(color = "black", fill = "#ffffff"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    #panel.background = element_rect(fill = "white"),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    #Axes
    axis.title = element_text(size = 40, family = "bold", color = "black"),
    axis.text.y = element_text(size = 30, family = "regular", color = "black"),
    axis.text.x = element_text(size = 30, family = "regular", color = "black"),
    axis.ticks = element_line(color = "black"),
    axis.line = element_blank(),
    #Plus
    text = element_text(family = "regular", size = 35),
    legend.text = element_text(size=30, family="regular"),
    legend.position = "top",
    legend.key = element_blank(),
    legend.key.width = unit(0.2, "cm")) 

ggsave("xyplot_nec_rev.png",
       plot=xyplot,
       device = agg_png(width = 6.5, height = 6, units = "in", res = 300))